<?php
session_start();
require_once 'config/database.php';

if (!isset($_SESSION['user_id']) || !isset($_POST['car_id'])) {
    header('Location: login.php');
    exit();
}

$database = new Database();
$db = $database->getConnection();

$user_id = $_SESSION['user_id'];
$car_id = $_POST['car_id'];
$preferred_date = $_POST['preferred_date'];
$preferred_time = $_POST['preferred_time'];

$query = "INSERT INTO test_drives (user_id, car_id, preferred_date, preferred_time) 
          VALUES (?, ?, ?, ?)";
$stmt = $db->prepare($query);

if ($stmt->execute([$user_id, $car_id, $preferred_date, $preferred_time])) {
    $_SESSION['success_message'] = "Test drive scheduled successfully! We will contact you to confirm the appointment.";
} else {
    $_SESSION['error_message'] = "Failed to schedule test drive. Please try again.";
}

header('Location: car-details.php?id=' . $car_id);
exit();
?> 